C Versus C++
C and C++ are essentially the same language Because C++ is built on the ground laid by C, the languages are very simmilar. In fact, all C code can be used and compiled in a C++ program. However, there are several key differences between the two languages. You can declare variables anywhere. Unlike C, C++ will allow you to declare variables anywhere - not just the top of a function. One of the most important uses of this new functionality is in for loops. Instead of having to declare a variable "i" for use in a loop, the syntax can be streamlined as such: for (int i = 0; i < 5; i++) cout << "Wow! How easy!"; Note that while this new technique makes writing loops much quicker, it also limits the scope of the variable "i" to the for loop. That is, "i" cannot be used anywhere outside of the loop. As such, it may sometimes be necessary to declare the variable outside of the for loop as one would in the C. Casting In C++, type casting can be used in the same way as in C, but there is also a new syntax which can be used. int x = 4; (float)x; // Cast the int x to a float in the style of C float(x); // Cast the int x to a float in the style of C++ Which type of casting you use is purely based on style and preference, though the C++ style can be more clear in some cases. Const Making a variable const means that it is immutable (cannot be changed). From the compiler's viewpoint, a const variable cannot appear on the left hand side of an assignment (as these would mean that its value would be changed). The const keyword always applies to the left nearest entity, and otherwise the right. The folllowing examples clarify this meaning (example taken from Piazza post by Vicky). int const *a; // The pointer/address is changeable, but the value // it points to is constant. const int *a; // Same as the first one. int* const a; // The pointer cannot point to anywhere else once assigned, // but the value *a can be changed (The pointer is constant). const int* const a; // Both the integer and its address are constant. In addition, function parameters can be declared as const. A variable that is const can only be passed to a function with a const parameter. One last note on const is that it can be used in a method definition of a class to denote that the object calling the method will not itself be changed. Such a function would be in the form: const int MyClass::foo (const double boo) const { ... } The code above defines a member function of the class MyClass named foo, which returns a const integer, takes a const double as a parameter, and does not modify the object calling the method. Name Mangling Name mangling is a fancy name used to describe the fact that in C++ multiple functions can have the same name. Name mangling refers to the fact that in order to allow this, the compiler "mangles" the name of a function based on its definition. That is, while the function defined by "int foo ()" has the name foo, a compiler might see this function as being called intfoo. Because the compiler's new name for the function includes information about its parameters and return type, multiple functions can have the same name - so long as they differ by at least one parameter or return type. This is a very useful fact about C++, and is used quite often. One common use is in constructors for a class. Default Arguments In C++, a function can have default arguments. The syntax for this is as follows: void foo (char* text, int num = 0) { ... } The code above defines a function named foo which takes a char pointer and an integer as parameters. While the function could be called with both a char pointer and an int, if it were called with only a char pointer [ as in foo (str); ], the parameter num would automatically take on the value 0. Note that default values must be on the right side of a function def inition. That is void foo (char* text = NULL, int num) { ... } would not compile, because the default parameters do not begin on the right hand side of the parameter list. #define While in C it is common to use #define to substitute a constant value, the const keyword is preferred in C++ #define MAX = 12 const int MAX = 12; // Preferred in C++ Because C++ is a strongly typed language (everything has a type), the second method is preferred as it allows the compiler to do its job and check if constant values are kept unchanged. I/O (Input / Output) In C++, the the functionality of the stdio.h library is replaced by the iostream library. For basic input and output, the scanf and printf functions have been replaced by the cin and cout objects. The syntax will be deomonstrated in the following examples: //Some basic C output #include int main() { int foo = 4; printf("This is a simple print statement.\n"); printf("This prints out an integer %d\n", foo); printf("This prints out two integers %d, %d\n", 12, foo); scanf("%d", foo); // Scans an integer and stores it in foo return 0; } //Same code in C++ #include using namespace std; // You need this line to use standard C++ libraries int main() { int foo = 4; cout << "This is a simple print statement." << endl; cout << "This prints out an integer " << foo << endl; cout << "This prints out two integers " << 12 << ", " << foo << endl; cin >> foo; // Scans an integer and stores it in foo return 0; } Notice that the \n has been replaced by endl. The newline character still works in C++, and it is purely stylistic which method you choose to use for newlines. Malloc/Free and New/Delete In C++, the functionality of malloc and free have been replaced by the keywords new and delete. These new methods of managing memory eliminate much of the grunt work associated with malloc and free. Other than the syntax, these new methods work in the exact same way as their C counterparts. int* numbers = new int10; // Creates an array of 10 integers int* number = new int; // Creates one integer delete [] numbers; // Frees the memory from an array of numbers (note the brackets) delete number; // Frees the memory from one number